package cn.com.sbell.vss.auth;

import javax.servlet.http.HttpServletRequest;

import cn.com.sbell.vss.ICommander;
import cn.com.sbell.vss.ILogCommander;
import cn.com.sbell.vss.Response;
import cn.com.sbell.vss.VissSession;
import cn.com.sbell.vss.VssConstants;
import cn.com.sbell.vss.VssException;
import cn.com.sbell.vss.util.SessionHolder;

/**
 * @author Yifu.HUANG
 * 
 */
public class UserLogin implements ICommander, ILogCommander {
	private SessionHolder sessionHolder;

	private ICommander emsUserLogin;

	private ICommander csgUserLogin;

	private ICommander maUserLogin;

	/**
	 * (non-Javadoc)
	 * 
	 * @see cn.com.sbell.vss.ICommander#execute(javax.servlet.http.HttpServletRequest)
	 */
	public Response execute(HttpServletRequest request) {
		VissSession session = (VissSession) request
				.getAttribute(VssConstants.KEY_USER_SESSION_DATA);
		if (session != null) {
			sessionHolder.removeSession(session.getSessionId());
			request.removeAttribute(VssConstants.KEY_USER_SESSION_DATA);
		}
		if (emsUserLogin != null) {
			return emsUserLogin.execute(request);
		} else if (csgUserLogin != null) {
			return csgUserLogin.execute(request);
		} else if (maUserLogin != null) {
			return maUserLogin.execute(request);
		} else {
			throw new VssException(VssConstants.CODE_UNSUPPORTED_COMMAND_ERROR);
		}
	}

	public void setEmsUserLogin(ICommander emsUserLogin) {
		this.emsUserLogin = emsUserLogin;
	}

	public void setCsgUserLogin(ICommander csgUserLogin) {
		this.csgUserLogin = csgUserLogin;
	}

	public void setMaUserLogin(ICommander maUserLogin) {
		this.maUserLogin = maUserLogin;
	}

	public void setSessionHolder(SessionHolder sessionHolder) {
		this.sessionHolder = sessionHolder;
	}
}
